// index.js
const CryptoJS = require('../../utils/crypto-js.js')

Page({
  data: {
    itrahost:"https://itra.run",
    title:"",
    name:'',
    loading:false,
    noMore:false,
    start:1,
    firstname:'',
    lastname:'',
    pageNum:1,
    pageSize:10,
    currentIndex:0,
    isback:0,
    list: []
  },
   
  onLoad:function(options){
    var obj =JSON.parse(decodeURIComponent(options.list))
    this.setData({
      list:obj,
      name:options.name,
      title:options.name,
      firstname:options.firstname,
      lastname:options.lastname,
      noMore:obj.length<this.data.pageSize,
      loading:false,
      isback:options.isback
    })
  },
   
  onCopy:function(e){
    var index = e.currentTarget.id;
    var obj = this.data.list[index];
    wx.setClipboardData({
      data: obj.RunnerId,
      success (res) {
        wx.showToast({
          title: 'ITRA ID已复制',
          icon:"none",
          duration:2500
        })
      }
    })
  },
  onItem:function(e){
      var index = e.currentTarget.id;
      var obj = this.data.list[index];
      var info = {
        'firstName':obj.firstName,
        'lastName':obj.lastName,
        'runnerId':obj.runnerId,
        'gender':obj.gender,
        'pi':obj.pi,
        'nationality':this.data.itrahost+obj.code,
        'profilePic':this.data.itrahost+obj.profilePic
      };
      var str = encodeURIComponent(JSON.stringify(info));
      wx.navigateTo({
        url: '../mine/mine?str='+str
      })
    },
  
  //ITRA助手请求数据
  requestITRAPrefData:function(){
    const app = getApp();
    var name = this.data.lastname+" "+this.data.firstname;
    wx.request({
      url: app.globalData.searchUrl,
      data:{
        "name":name,
        "pageNum":this.data.pageNum,
        "pageSize":10,
        'echoToken':0.009322270910500974
      },
      header:{
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
      },
      method:"POST",
      success:(res)=>{
        this.setData({
          loading: false
        });
        var data = res.data;
        // console.log(response);
        // var ciphertext_b64 = response.response1;
        if(data.code==1){
            // var iv_b64 = response.response2;
            // var key_b64 = response.response3;
            // const key = CryptoJS.enc.Base64.parse(key_b64);
            // const iv = CryptoJS.enc.Base64.parse(iv_b64);
            // const ciphertext = CryptoJS.enc.Base64.parse(ciphertext_b64);
            // const decrypted = CryptoJS.AES.decrypt(
            //     { ciphertext: ciphertext },
            //     key,
            //     {
            //       iv: iv,
            //       mode: CryptoJS.mode.CBC,
            //       padding: CryptoJS.pad.Pkcs7
            //     }
            //   );
            // const plaintext = decrypted.toString(CryptoJS.enc.Utf8);
            // const jsonData = JSON.parse(plaintext);
            var dataVa = data.data;
           
            var results = dataVa.results;
            console.log('=====len=='+results.length);
            if(results.length<this.data.pageSize){
              this.setData({
                loading: false,
                noMore:true,
              });
            }else{
              //results
              this.setData({
                list:this.data.list.concat(results),
                noMore:results.length<this.data.pageSize,
                loading:false,
              })
            }
        }else{
          this.setData({
            loading: false,
            noMore:true,
          });
        }
        
      },
      fail: (err) => {//请求失败
        this.setData({
          loading: false
        });
        wx.showModal({
          title: '温馨提示',
          content: "网络较慢,数据请求失败!",
          showCancel:false,
          confirmText:"确定",
          confirmColor:"#000000"
        })
      }
    })
  },
  onScrollToLower: function() {
    console.log('=====b==b==b==b==');
    if(this.data.noMore) return;
    if(this.data.loading) return;
    this.data.pageNum = this.data.pageNum+1;
    this.setData({
      loading:true,
    })
    this.requestITRAPrefData();
  },


  bindItemChange(e){
      this.setData({
        currentIndex:e.detail.current
      })
  },
  
})
